package Phone;

import Phone.*;
import java.util.*;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * This is a sample class to launch a rule.
 */
public class Drooled_Phones {

	static ArrayList<Phone> phones = new ArrayList<Phone>();
	static HashMap<EProperty, String> EPropertyPytanie = new HashMap<EProperty, String>();
	static int activeEnum=-1;
	static EProperty properties[];
	static String command="n";
	static boolean koniec = false;
	
	public static final void main(String[] args) {
		 
		 boolean koniec = false;
	     Scanner in = new Scanner(System.in);
	     CreatePhonesBase();
	     CreateQuestionHashmap();
	     try {
				KnowledgeBase kbase = readKnowledgeBase();
				StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
				KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
			
				while(true){
					NextQuestion();
					if(koniec)
						break;
				    command = in.nextLine();
				    if(command.equals("w")){
				    	PrintPhones();
				    }else if(command.equals("n")){
				    }else if(command.equals("t")){ 
				    	CProperty fact = new CProperty();
						fact.setProp(properties[activeEnum]);
						ksession.insert(fact);
						ksession.fireAllRules();
				    }else if(command.equals("k")){
				    	koniec = true;
				    }    	 
				}
				logger.close();
	     } catch (Throwable t) {
				t.printStackTrace();
	     }
		in.close();          
	}
	
	private static void CreateQuestionHashmap(){
		EPropertyPytanie.put(EProperty.DOTYKOWY, new String("Czy zalezy ci aby twoja komorka Miala ekran dotykowy ?"));
		EPropertyPytanie.put(EProperty.VIDEO, new String("Czy zalezy ci aby twoja komorka by byla w stanie nagrywac filmy?"));
		EPropertyPytanie.put(EProperty.UMTS, new String("Czy zalezy ci aby twoja komorka posiadala UMTS ?"));
		EPropertyPytanie.put(EProperty.GPS, new String("Czy zalezy ci aby twoja komorka posiadala GPS ?"));
		EPropertyPytanie.put(EProperty.BIGSCR, new String("Czy zalezy ci aby twoja komorka miala duzy ekran ?"));
		EPropertyPytanie.put(EProperty.QWR, new String("Czy zalezy ci aby twoja komorka posiadala klawiature qwerty ?"));
		EPropertyPytanie.put(EProperty.VP, new String("Czy zalezy ci aby twoja komorka mogla odtwarzac filmy ?"));
		EPropertyPytanie.put(EProperty.DOBRA_BATERIA, new String("Czy zalezy ci aby twoja komorka mogla dlugo pracowa na baterii"));
		EPropertyPytanie.put(EProperty.DOBRY_APARAT, new String("Czy zalezy ci aby twoja komorka miala dobry aparat?"));
		EPropertyPytanie.put(EProperty.DUZA, new String("Czy zalezy ci aby twoja komorka byla duza ?"));
		EPropertyPytanie.put(EProperty.SREDNIA, new String("Czy zalezy ci aby twoja komorka byla sredniej wielkosci ?"));
		EPropertyPytanie.put(EProperty.MALA, new String("Czy zalezy ci aby twoja komorka byla mala ?"));
		EPropertyPytanie.put(EProperty.SLIDER, new String("Czy zalezy ci aby twoja komorka miala slider ?"));
		EPropertyPytanie.put(EProperty.KLAPKA, new String("Czy zalezy ci aby twoja komorka byla z klapka ?"));
		EPropertyPytanie.put(EProperty.STANDARD, new String("Czy zalezy ci aby twoja komorka miala klasyczna obudowe ?"));
		EPropertyPytanie.put(EProperty.TANIA, new String("Czy zalezy ci aby twoja komorka byla tania ?"));
		EPropertyPytanie.put(EProperty.SREDNIODROGA, new String("Czy zalezy ci aby twoja komorka byla sredniodroga ?"));
		EPropertyPytanie.put(EProperty.BARDZO_DROGA, new String("Czy zalezy ci aby twoja komorka byla bardzo droga ?"));
		properties = EProperty.values();
	}
	
	
	private static void NextQuestion(){
		if(phones.isEmpty()){
			System.out.println("Nie ma wiecej komorek w bazie jestes zbyt wybredny !");
			koniec = true;
			return;
		}
		if(phones.size() == 1){
			System.out.println("Pozostala jedna komorka:");
			PrintPhones();
			koniec = true;
			return;
		}
		System.out.println("Zeby wyswietlic komorki wpisz \"w\" aby zakonczyc program wcisnij \"k\" ");
		System.out.println("odpowiadaj na pytania \"t\" jako tak i \"n\" jako nie");
		if(command.equals("w")){
			System.out.println(EPropertyPytanie.get(properties[activeEnum]));
			
		}else{
			if(command.equals("t")){
				if(activeEnum == 9){
					activeEnum+=3;
				}else if(activeEnum ==10){
					activeEnum+=2;
				}else if(activeEnum ==12){
					activeEnum+=3;
				}else if(activeEnum ==13){
					activeEnum+=2;
				}else if(activeEnum ==15){
					activeEnum+=3;
				}else if(activeEnum ==16){
					activeEnum+=2;
				}else{
					activeEnum++;
				}
				if(activeEnum >= properties.length){
					koniec = true;
					System.out.println("Koniec pytan pozostaly komorki: ");
					PrintPhones();
					return;
				}
				System.out.println(EPropertyPytanie.get(properties[activeEnum]));
			}else if(command.equals("n")){
				activeEnum++;
				if(activeEnum >= properties.length){
					koniec = true;
					System.out.println("Koniec pytan pozostaly komorki: ");
					PrintPhones();
					return;
				}
				System.out.println(EPropertyPytanie.get(properties[activeEnum]));
			}
		}
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error: errors) {
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		
		return kbase;
	}
	
	public static void SafePhones(EProperty prop){
		Iterator<Phone> iterator = phones.iterator();
		while(iterator.hasNext()){
			Phone nextPhone = iterator.next();
			if(!nextPhone.hasProp(prop)){
				phones.remove(nextPhone);
				iterator = phones.iterator();
			}
		}
	}
	
	private static void CreatePhonesBase(){
		phones.clear();
		AddPhone("Nokia 3110 C", new EProperty[]{EProperty.TANIA, EProperty.VIDEO, EProperty.MALA,EProperty.STANDARD });
		AddPhone("SE C510", new EProperty[]{EProperty.TANIA, EProperty.UMTS, EProperty.VIDEO, EProperty.VP, EProperty.MALA, EProperty.STANDARD});
		AddPhone("Nokia 6700C", new EProperty[]{EProperty.SREDNIODROGA, EProperty.UMTS, EProperty.GPS, EProperty.DOBRY_APARAT, EProperty.VIDEO, EProperty.VP, EProperty.MALA, EProperty.STANDARD});
		AddPhone("Nokia E75", new EProperty[]{EProperty.SREDNIODROGA, EProperty.UMTS, EProperty.GPS, EProperty.VIDEO, EProperty.BIGSCR, EProperty.QWR, EProperty.VP, EProperty.SREDNIA, EProperty.SLIDER});
		AddPhone("Nokia N900", new EProperty[]{EProperty.BARDZO_DROGA, EProperty.DOTYKOWY, EProperty.UMTS, EProperty.GPS, EProperty.DOBRY_APARAT, EProperty.VIDEO, EProperty.BIGSCR, EProperty.QWR, EProperty.VP, EProperty.DUZA, EProperty.SLIDER});
		AddPhone("Samsung C260", new EProperty[]{EProperty.TANIA, EProperty.MALA, EProperty.KLAPKA});
		AddPhone("Samsung B2100", new EProperty[]{EProperty.TANIA, EProperty.DOBRA_BATERIA, EProperty.VP, EProperty.SREDNIA, EProperty.STANDARD});
		AddPhone("SE W880", new EProperty[]{EProperty.TANIA, EProperty.UMTS, EProperty.VIDEO, EProperty.VP, EProperty.MALA, EProperty.STANDARD});
		AddPhone("HTC Hero", new EProperty[]{EProperty.BARDZO_DROGA, EProperty.DOTYKOWY, EProperty.UMTS, EProperty.GPS, EProperty.DOBRY_APARAT, EProperty.VIDEO, EProperty.BIGSCR, EProperty.VP, EProperty.SREDNIA, EProperty.STANDARD});
		AddPhone("Nokia 6650", new EProperty[]{EProperty.SREDNIODROGA, EProperty.UMTS, EProperty.DOBRA_BATERIA, EProperty.GPS, EProperty.VIDEO, EProperty.VP, EProperty.MALA, EProperty.KLAPKA});
		AddPhone("Nokia 3310", new EProperty[]{EProperty.TANIA, EProperty.SREDNIA, EProperty.STANDARD});
		AddPhone("Nokia 5200", new EProperty[]{EProperty.TANIA, EProperty.VIDEO, EProperty.VP, EProperty.SREDNIA, EProperty.STANDARD});
		AddPhone("Nokia N70", new EProperty[]{EProperty.TANIA, EProperty.UMTS, EProperty.GPS, EProperty.VIDEO, EProperty.BIGSCR, EProperty.VP, EProperty.DUZA, EProperty.STANDARD});
		AddPhone("Samsung Avila", new EProperty[]{EProperty.TANIA, EProperty.DOTYKOWY, EProperty.VIDEO, EProperty.BIGSCR, EProperty.VP, EProperty.SREDNIA, EProperty.STANDARD});
		AddPhone("Nokia e52", new EProperty[]{EProperty.SREDNIODROGA, EProperty.UMTS, EProperty.GPS, EProperty.VIDEO, EProperty.BIGSCR, EProperty.VP, EProperty.MALA, EProperty.STANDARD});
		AddPhone("BlackBerry  8520", new EProperty[]{EProperty.SREDNIODROGA, EProperty.VIDEO, EProperty.BIGSCR, EProperty.QWR, EProperty.VP, EProperty.DUZA, EProperty.STANDARD});
		AddPhone("BlackBerry 9700", new EProperty[]{EProperty.BARDZO_DROGA, EProperty.UMTS, EProperty.GPS, EProperty.VIDEO, EProperty.BIGSCR, EProperty.QWR, EProperty.VP, EProperty.DUZA, EProperty.STANDARD});
	}
	
	private static void PrintPhones(){
		Iterator<Phone> iterator = phones.iterator();
		while(iterator.hasNext()){
			Phone nextPhone = iterator.next();
			nextPhone.PrintOut();
		}
	}
	
	private static void AddPhone(String name, EProperty[] props){
		ArrayList<EProperty> properties = new ArrayList<EProperty>();
		for(int i =0 ; i < props.length ; i++){
			properties.add(props[i]);
		}
		phones.add(new Phone(name, properties));
	}
}

